

<?


class CTRLsocial extends CONTROL {

	function __construct( $vars ) {
		global $Object;
		
		$this->_addvars( $vars );

		if( $this->action != "list" )
			$Object = $this->create_object();
		
	}
	
	var $stream_sources = array( "youtube"=>"youtube", "vimeo"=>"vimeo" );

#	Create social object.
	function create_object() {
		global $Engine, $Object;
		
		return $Object = ( $this->action == "new" )?
			new OBJECT( $this->vars, 0, "..." ) : new OBJECT( $this->vars );
	
	}
	
	
	function render_list( $request ) {
		global $DB;
	
		$keys	= $DB->query_rows( 'SELECT * FROM `types` WHERE `key` LIKE  "%social.%" AND `table` = "mod_social"' );	
		foreach( $keys as $key )
			$types[]	= $key[ 'key' ];
					
		$request->vars->type			= $types;
		$request->vars->flow->current	= (( $request->vars->flow->current ) ? $request->vars->flow->current : 0 );
		$request->vars->flow->limit		= (( $request->vars->flow->limit ) ? $request->vars->flow->limit : 24 );
		
		$types = array_values( $DB->types( $types ));
			
		if( $request->vars->flow->order ) foreach( $request->vars->flow->order as $key=>$val ) $order[]= "$key $val";
		else $order[] = "ID desc";
		
		$list  = $DB->query_rows( "select ID, llog, type, nameid, rank, ID as key_0, <%lang:nameid%> as key_1, type as key_2 
			from mod_social where type in (" . implode( ', ', $types ) . ") and active = " . $request->vars->reqs->active . " 
			ORDER BY ". implode(',', $order ) ."  
			LIMIT ". $request->vars->flow->current .", ". $request->vars->flow->limit );
			
		$count = $DB->query_direct( 'select count(*) from mod_social 
			where type in (' . implode( ', ', $types ) . ') and active = ' . $request->vars->reqs->active . ' ORDER BY ID DESC' );			
		
		if( $list ) {
			foreach( $list as $item ) {
				$li	= array();
				$li[ 'id' ]		= $item[ 'ID' ];
				$li[ 'type' ]	= $DB->type_r( $item[ 'type' ] );
				$li[ 'rank' ]	= (( !$item[ 'rank' ] ) ? 0 : $item[ 'rank' ] );
				$li[ 'name' ]	= $item[ 'ID' ];
				$li[ 'list' ]	= array( $item[ 'ID' ], $DB->type_r( $item[ 'key_2' ] ), $item[ 'llog' ] );
				$info[] = (object) $li;
			}
		
			$listing	= array(
				'limit'		=> (( $request->vars->flow->limit ) ? $request->vars->flow->limit : 24 ),
				'current'	=> (( $request->vars->flow->current ) ? $request->vars->flow->current : 0 ),
				'total'		=> $count
			);
			
			return array( 'list' => $info, 'listing' => $listing );
		} else {
			
			return "
			ref.seed([
				new Element( 'h1' ).update( '" . ucfirst( $this->vars->descr ) . "' ),
				new Element( 'span' ).update( 'nothing here...' )
			]);";
		
		}
	}
	
#	Filter Flow request.
	function filter_flow() {
		global $DB, $Request;
				
		$keys	= $DB->query_rows( 'SELECT * FROM `types` WHERE `key` LIKE  "%social.%" AND `table` = "mod_social"' );	
		foreach( $keys as $key )
			$types[]	= $key[ 'key' ];
		
		$types = array_values( $DB->types( $types ));
		$count = $DB->query_direct( 'select count(*) from mod_social where type in (' . implode( ', ', $types ) . ') and active = 1 ORDER BY ID DESC' );			

		if( !$flow = $Request->vars->flow) $flow = (object) array();
		if( $Request->vars->reqs ) foreach( $Request->vars->reqs as $key=>$val ) $Request->flowreqs[] = "$key='$val'";
		if( !$Request->flowreqs ) $Request->flowreqs[] = "ID>0";
	
		$flow->total = $count;

		if( $Request->direction == "prev" )
			$flow->current = ( !$flow->current && $flow->limit < $flow->total )? $flow->total - $flow->limit : (( $flow->limit > $flow->current )? 0: $flow->current - $flow->limit );
		else if( $Request->direction == "next" )
			$flow->current = ( $flow->current < $flow->total - $flow->limit)? $flow->current + $flow->limit : 0;
		else if( !$flow->current ) $flow->current = 0;
		
		//print_r( $flow );
		
		return "$('control_list')._push( ref, " . json_encode( $flow ) . " );";
	}


#	List update.
	function filter_update_list() {
		global $Request;
		
		//@print_r( $Request );
		
		$res	= $this->render_list( $Request );		
		return "ref.seed( ". json_encode( $res[ 'list' ] ) . "," . json_encode( $res[ 'listing' ] ) . "," . json_encode( array( 'active' => 1 )) . ");";
	}

#	Object list.
	function filter_list() {
		global $DB, $Request;
		
		if( $keys	= $DB->query_rows( 'SELECT * FROM `types` WHERE `key` LIKE  "%social.%" AND `table` = "mod_social"' )) {
			
			$res = $this->render_list( $Request );
			
			return "
			ref.seed([
				new Element( 'h1' ).update( '" . ucfirst( $this->vars->descr ) . "' ),
				lst = new List({ type: ref.vars.type, descr:'" . (( $this->vars->boxdescr )? $this->vars->boxdescr : $this->vars->descr ) . "'})
			]);
		
			lst.seed( ". json_encode( $res[ 'list' ] ) . "," . json_encode( $res[ 'listing' ] ) . "," . json_encode( array( 'active' => 1 )) . ");";
		
		} else {
			
			return "
			ref.seed([
				new Element( 'h1' ).update( '" . ucfirst( $this->vars->descr ) . "' ),
				new Element( 'span' ).update( 'nothing here...' )
			]);";
		}
	}

#	Edit Object.

	// case for different media filters
	function filter_edit() {
	
		return $this->{ "filter_edit_" . array_pop( explode( ".", $this->vars->type ))}();
	
	}
	
	// filter media 
	function filter_edit_img() {
		global $Engine, $Object;
		
		return "
		ref.seed([
			
			new Element( 'h1' ).update( \"Beheer {$this->vars->type} '". (( $Object->get( "nameid" ) )? $Engine->text->get( $Object->get( "nameid" ) ) : $Object->id ) . "'\" ),
			new Textfield({ descr:'name {$this->vars->type}', type:'{$this->vars->type}', parentid:{$Object->id}, textid:". $Object->get( "nameid" ) .", values:" . 
				json_encode( $Engine->text->filter( $Object->get( "nameid" ) )) ." }),
			new Htmlfield({ descr:'text {$this->vars->type}', type:'{$this->vars->type}', parentid:{$Object->id}, textid:". $Object->get( "textid" ) .", values:" . 
				json_encode( $Engine->text->filter($Object->get( "textid" ), 'long' )) ." })
		]);";

	}
	
	// filter blog
	function filter_edit_blog() {
		global $Engine, $Object;
		
		return "
		ref.seed([
			
			new Element( 'h1' ).update( \"Beheer {$this->vars->type} '". (( $Object->get( "nameid" ) )? $Engine->text->get( $Object->get( "nameid" ) ) : $Object->id ) . "'\" ),
			new Textfield({ descr:'name {$this->vars->type}', type:'{$this->vars->type}', parentid:{$Object->id}, textid:". $Object->get( "nameid" ) .", values:" . 
				json_encode( $Engine->text->filter( $Object->get( "nameid" ) )) ." }),
			new Htmlfield({ descr:'text {$this->vars->type}', type:'{$this->vars->type}', parentid:{$Object->id}, textid:". $Object->get( "textid" ) .", values:" . 
				json_encode( $Engine->text->filter($Object->get( "textid" ), 'long' )) ." }),
			new Options({ descr:'Informatie', active:true, floats: [
				new Shift({ type:'{$this->vars->type}', descr:'active', value:{$Object->get( 'active' )}, field:'active', objid:{$Object->id} })
			]})
		]);";

	}
	
	// filter album
	function filter_edit_album() {
		global $Engine, $Object;
		
		return "
		ref.seed([
			
			new Element( 'h1' ).update( \"Beheer {$this->vars->type} '". (( $Object->get( "nameid" ) )? $Engine->text->get( $Object->get( "nameid" ) ) : $Object->id ) . "'\" ),
			new Textfield({ descr:'name {$this->vars->type}', type:'{$this->vars->type}', parentid:{$Object->id}, textid:". $Object->get( "nameid" ) .", values:" . 
				json_encode( $Engine->text->filter( $Object->get( "nameid" ) )) ." }),
			new Options({ descr:'Informatie', active:true, floats: [
				new Ajxfield({ type:'{$this->vars->type}', parenttype: '" . $Object->parenttype . "', parentid: '" . $Object->parentid . "', descr:'source', ref:ref.id, id:{$Object->id}, field:'meta.source', value:'{$Object->get('source')}' }),
				new Ajxfield({ type:'{$this->vars->type}', parenttype: '" . $Object->parenttype . "', parentid: '" . $Object->parentid . "', descr:'id', ref:ref.id, id:{$Object->id}, field:'meta.id', value:'{$Object->get('id')}' }),
				new Shift({ type:'{$this->vars->type}', descr:'active', value:{$Object->get( 'active' )}, field:'active', objid:{$Object->id} })
			]})
		]);";

	}
	
	// filter streams
	function filter_edit_stream()  {
		global $DB, $Engine, $Object;
		
		foreach( $this->stream_sources as $key => $value ) {
			$options[] = array( "key" => $key, "value"=> $key );
		}
				
		return "
		ref.seed([
			
			new Element( 'h1' ).update( \"Beheer {$this->vars->type} '". (( $Object->get( "nameid" ) )? $Engine->text->get( $Object->get( "nameid" ) ) : $Object->id ) . "'\" ),
			new Element( 'span', {id: ref.id + '.name'} ).update( '{$Object->get( 'name' )}'),
			new Options({ descr:'Informatie', active:true, floats: [
				new Select({ type: '{$this->vars->type}', descr:'type mediaprovider', objid: {$Object->id}, field:'meta.source', key: '{$Object->get( 'source' )}', opts: " . json_encode( $options ) . " }),
				new Ajxfield({ type:'{$this->vars->type}', parenttype: '" . $Object->parenttype . "', parentid: '" . $Object->parentid . "', descr:'source id', ref:ref.id, id:{$Object->id}, field:'meta.id', value:'{$Object->get('id')}', requestfunc:'pull' }),
				new Shift({ type:'{$this->vars->type}', descr:'active', value:{$Object->get( 'active' )}, field:'active', objid:{$Object->id} })
			]})
		]);";
	}


	
#	Shift (alter column value).
	function filter_ajax() {
		global $DB, $Request;
		
		if( $Request->vars->requestfunc )
			$comment = $this->{ $Request->vars->requestfunc }();
		
		else 
			$comment = ( $Request->ajax() )? "option modified":"warning: failed";

		return $this->comment( $comment );
	}
	
#	Social function: publish	
	function publish() {
		global $Request, $Object;
		
		if ($Request->vars->value == 1) 
			return $Object->publish( $Request->vars );
			
		else 
			return $Object->unpublish( $Request->vars );
		
	}
	

}




?>





